package com.garmin.tw.garminpcsync.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.garmin.tw.garminpcsync.android.adapter.LazyAdapter;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Tracker;

public class UpgradeFilesListViewActivity extends ListActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String [] skuList = new String [] {};
		String [] versionList = new String [] {};
		String [] downloadUrlList = new String [] {};
		
		if (savedInstanceState != null) {
			skuList = savedInstanceState.getStringArray("skuList");
			versionList = savedInstanceState.getStringArray("versionList");
			downloadUrlList = savedInstanceState.getStringArray("downloadUrlList");
		} else {
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				skuList = extras.getStringArray("skuList");
				versionList = extras.getStringArray("versionList");
				downloadUrlList = extras.getStringArray("downloadUrlList");
			}
		}
		
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (int i=0; i<skuList.length; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("lastestSku", skuList[i]);
			map.put("downloadUrl", downloadUrlList[i]);
			map.put("lastestVersion", versionList[i]);
			
			list.add(map);
		}
		
		//this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, updateResult));
		LazyAdapter adapter = new LazyAdapter(this, list);
		
		ListView lv = getListView();
		lv.setAdapter(adapter);
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// Get current context.
				Context mCtx = UpgradeFilesListViewActivity.this;
				// setContext will use mCtx to retrieve the application context.
				EasyTracker.getInstance().setContext(mCtx);
				// EasyTracker is now ready for use.
				Tracker myTracker = EasyTracker.getTracker();
				
				TextView title = (TextView)view.findViewById(R.id.title);
				
				myTracker.trackEvent("Check Software Upgrade Result Page", " Download Software", null, null);
				
				// When clicked, show a toast with the TextView text
				Toast.makeText(getApplicationContext(), title.getText(), Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	@Override
	public void onStart() {
		super.onStart();
		// ... // The rest of your onStart() code.
		EasyTracker.getInstance().activityStart(this); // Add this method.
	}

	@Override
	public void onStop() {
		super.onStop();
		// ... // The rest of your onStop() code.
		EasyTracker.getInstance().activityStop(this); // Add this method.
	}
}
